This lesson will explore the basics of using the ChatGPT API in Python. The ChatGPT API allows us to interact with the powerful ChatGPT language model, enabling us to generate conversational responses for various applications. In this lesson, we’ll understand how to make API requests to ChatGPT and retrieve responses using Python.

svg viewer

Let’s dive deep into setting up the environment for the API.

Setting up the environment#

Follow the steps below to set up a Python environment and generate OpenAI’s API credentials.

  • Install the Requests library using pip install requests.
  • Go to the OpenAI official website and create an account (if you still need to).
  • Navigate to the API section and generate an API key for the ChatGPT API.
  • Make sure to store the API key securely.

Making API requests#

In this section, we'll learn how to interact with the ChatGPT API by requesting and receiving responses. We'll explore the process of sending prompts to the API and receiving model-generated outputs.

API request
API request
  • Importing the required libraries:

Import libraries
  • Defining the API endpoint and headers:

API endpoints and headers

Note: Replace YOUR_API_KEY with your actual API key.

  • Constructing the payload:

Payload

The payload consists of a list of messages, where each message has a role (system or user) and content. The roles of the system and the user in message classification distinguish between AI-generated and user-input content.

  • Sending the API request:

API request

Sending a POST request using the POST method from the Request library.

  • Handling the API response:

Response handling

The above code shows how to receive and display a response to the user.

Building a ChatGPT interaction loop#

In this section, we’ll explore the concept of an interaction loop and learn how to create a continuous and interactive conversation with ChatGPT. An interaction loop is an iterative process of sending user messages as input and receiving model-generated responses. We’ll set up and maintain an interaction loop that allows us to have dynamic conversations with the model.

  • Construct an initial message:

Initial message

In line 1, a payload JSON object is defined with initial messages for each role.

  • Implement a response loop:

Response loop

Line 1: We define an infinite loop to iteratively process sending the user messages as input and receive model-generated responses.

Line 2: We send a request using API_ENDPOINT, HEADERS, and payload.

Line 3: We receive a response in the data variable using the json.load() method.

Lines 4–5: Replay from the model extracted for JSON format and display it in the next line.

Lines 7–8: We receive a new request from the user and append it to the payload list for the next iteration.

  • Defining the termination conditions:

Termination condition

Line 1: We implement a condition to end the chat if the user enters bye as input.

Let’s explore the syntax for error handling in the upcoming section.

Error handling and rate limiting#

By understanding error-handling techniques and implementing rate-limiting strategies, we can ensure a smoother and more reliable experience when interacting with the ChatGPT API.

  • Handling API errors:

API error

Lines 1–4: We check to see if response.status_code is not 200 (success). If it is not, the API must have failed, received the response text in error_data, and displayed it using print.

  • Implementing rate limiting:

To avoid exceeding the rate limits, you can add a sleep function between API requests:

Rate limiting

Let’s have a look at the complete code.

Putting it all together#

We’ll combine the knowledge and techniques we have to create a comprehensive implementation of ChatGPT using the API. This will enable us to develop a fully functional and interactive chatbot that leverages the power of ChatGPT for real-world applications.

Here’s an example of a chatbot script.

ChatGPT API usage

Line 11: A function, chat_with_gpt, is defined to interact with ChatGPT API. We combine the code of all the sections, completing the chatbot script.

Line 38: The chat_with_gpt function is called to test the script’s working.

We can also alter the model’s attributes to alter its behavior for our specific use case. The following section further expands on some key attributes that can influence the model’s behavior.

Control the model’s behavior#

Several variables govern the output generated by an LLM; changing these variables can make the model produce different outputs to the same input prompt. Suppose you want to write a poem and want the model to be more creative; to do this, you can alter the temperature variable. Increasing the temperature value can make the model act more creatively. The default temperature value for ChatGPT is 0.7.

The temperature parameter controls the randomness of the generated responses. A higher temperature value, such as 1.0, increases the randomness, resulting in more diverse and creative outputs. On the other hand, a lower temperature value, like 0.2, makes the responses more focused and deterministic. The temperature can be set as an input when making an API call to ChatGPT, allowing you to adjust the level of randomness based on your desired output.

Similarly, to control the range of tokens (text of output) that the model considers, you can use top-p and top-k. If we set the top-k value to 3, our model will only consider the top three tokens with maximum probabilities. Similarly, top-p is the sum of the probabilities of the tokens; for example, if the top-p-value is 0.15 and the sum of probabilities of the top two tokens is 0.14, then the model will only consider the top two tokens.

To set top-p (also known as nucleus sampling) and top-k, you can provide the values as parameters when making an API call to ChatGPT. The top-p value (e.g., 0.9) sets a threshold probability for the model’s predicted tokens, where only the most probable tokens collectively exceed the threshold are considered. The top-k value (e.g., 40) limits the selection of tokens to the top-k most probable tokens at each step.

Adjust the temperature parameter and top-k/top-p sampling techniques to control the randomness and diversity of the generated responses. Provide high-level instructions or context to guide the overall behavior of the model, such as instructing it to generate responses from a particular perspective or adopt a specific style.

To further understand, suppose we have a prompt asking, “What is your favorite color?” By setting top-p and top-k values, we can control the range of tokens the model considers for generating a response. For example, with top-p=0.8 and top-k=30, the model will prioritize the most likely tokens up to a cumulative probability of 0.8 and limit the selection to the top 30 most probable tokens.

This lesson taught us the basics of using the ChatGPT API in Python. We can now construct API requests, handle responses, and create an interactive conversation loop with ChatGPT. You can experiment with different payloads and explore the capabilities of ChatGPT to build powerful conversational applications. You can also alter ChatGPT’s variable to alter its behavior to your needs.

ChatGPT Usage

Basics of Prompting